package com.example.answer_staff;

import java.util.ArrayList;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.Animation;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.answer_staff.api.ApiManager;
import com.gaosai.staff.domain.MessageResult;
import com.gaosai.staff.utils.CommonUtils;
import com.gaosai.staff.utils.LogUtil;
import com.loopj.android.http.AsyncHttpResponseHandler;

public class VerifyActivity  extends Activity{

	private LinearLayout verify_lineraylayout_main;
	private LinearLayout verify_linearlayout_dynamic;
	private TextView verfiy_textview_cancle;
	private TextView verfiy_textview_edit;
	private Animation enter_lefttoright;
	private String[] enroll;
	private ArrayList<String> verify_list_key;
	private ArrayList<String> verify_list_value;
	private View activty_verify_bt;
	private String application_list;
	private ArrayList<String> enroll_bool_spilt_list;
	private String activity_id;
	private String activity_subject;
	private int activity_startTime;
	private int activity_endTime;
	private String activity_venue;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.verify);
		setupView();
		geographicallayout();
		setListens();
	}

	private void setupView() {
		verfiy_textview_cancle=(TextView)findViewById(R.id.verify_textview_cancel);
		verfiy_textview_edit=(TextView)findViewById(R.id.verify_textview_edit);
		application_list=getIntent().getStringExtra("application_list");
		enroll_bool_spilt_list=new ArrayList<String>();
		jsonParse(application_list);
		verify_list_key=	getIntent().getStringArrayListExtra("register_list_key");
		verify_list_value=getIntent().getStringArrayListExtra("register_list_value");
		activty_verify_bt=findViewById(R.id.activity_confirm_bt);
		TextView Verfiy_subject = (TextView)findViewById(R.id.Verfiy_subject);
		TextView Verfiy_startTime = (TextView)findViewById(R.id.Verfiy_startTime);
		TextView Verfiy_endTime = (TextView)findViewById(R.id.Verfiy_endTime);
		TextView Verfiy_venue = (TextView)findViewById(R.id.Verfiy_venue);
		Verfiy_subject.setText(activity_subject);
		Verfiy_startTime.setText(CommonUtils.getDateToStringt(activity_startTime));
		Verfiy_endTime.setText(CommonUtils.getDateToStringt(activity_endTime));
		Verfiy_venue.setText(activity_venue);
	}

	private void jsonParse(String json) {
		JSONObject jsonObject=null;
		try {
			jsonObject = new JSONObject( json);
			String enroll_need = jsonObject.getString("enroll_need");
			activity_id = jsonObject.getString("id");
			activity_subject = jsonObject.getString("subject");
			activity_startTime = jsonObject.getInt("startTime");
			activity_endTime= jsonObject.getInt("endTime");
			activity_venue= jsonObject.getString("venue");


			Spilt(enroll_need);
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	private void Spilt(String string) {
		enroll = string.split("==");
		Log.i("eee", "liatadapter中得enroll的长度是"+enroll.length);
		for (int i = 0; i < enroll.length; i++) {
			String enroll_s = enroll[i];
			String[] enroll_s_spilt = enroll_s.split(":");
			enroll_bool_spilt_list.add(enroll_s_spilt[1]);
		}
	}

	private void setListens() {
		verfiy_textview_cancle.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
//				startActivity(new Intent(VerifyActivity.this,RegisterActivity.class));
//				overridePendingTransition(R.anim.exit_lefttoright,  R.anim.enter_lefttoright);
				finish();
			}
		});
		verfiy_textview_edit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				//				overridePendingTransition( R.anim.exit_lefttoright,  R.anim.enter_lefttoright);
				finish();
			}
		});
		activty_verify_bt.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				StringBuilder sb = new StringBuilder();
				for(int i=0;i<enroll.length;i++){
					String boll = enroll_bool_spilt_list.get(i);
					Log.i("eee", "Verify的boll是"+boll);		
					String key = verify_list_key.get(i);
					Log.i("eee", "Verify的key是"+key);
					String value= verify_list_value.get(i);
					Log.i("eee", "Verify的valuue是"+value);
					sb.append(key).append(":").append(boll).append(":").append(value).append("==");
				}

				sb.deleteCharAt(sb.length() - 1);
				sb.deleteCharAt(sb.length() - 1);
				Log.e("sbbbbbbbbbb", "sb是"+sb.toString());

				ApiManager.getInstance().regist(getApplicationContext(), sb.toString(), activity_id, new AsyncHttpResponseHandler(){
					@Override
					public void onFailure(int arg0, Header[] arg1, byte[] arg2,
							Throwable arg3) {
					}
					@Override
					public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
						String result = new String(arg2) ;
						MessageResult messageResult = MessageResult.prase(result) ;
						if(0==messageResult.getCode()){
							LogUtil.e("joinActivity result", "joinActivity结果是"+result) ; 
							Toast.makeText(getApplicationContext(), result, 1).show();

							Intent intent=new Intent(VerifyActivity.this,ConfirmActivity.class);
							intent.putExtra("application_list", application_list);
							intent.putExtra("enroll", enroll);
							intent.putExtra("register_list_key",verify_list_key);
							intent.putExtra("register_list_value",verify_list_value);
							startActivity(intent);

						}else{
							Toast.makeText(VerifyActivity.this, messageResult.getMsg(), 0).show() ;
						}
					}
				}); 
			}
		});
	}
	private void geographicallayout() {
		for(int i=0;i<enroll.length;i++){
			verify_lineraylayout_main=(LinearLayout)findViewById(R.id.confirm_lineraylayout_main);
			verify_linearlayout_dynamic=(LinearLayout)LayoutInflater.from(this).inflate(R.layout.verify_textview_dynamic, verify_lineraylayout_main, false);
			TextView verify_dtextView_dynamic_key = (TextView)verify_linearlayout_dynamic.findViewById(R.id.verify_dtextView_dynamic_key);
			TextView verify_dtextView_dynamic_value = (TextView)verify_linearlayout_dynamic.findViewById(R.id.verify_dtextView_dynamic_value);
			verify_dtextView_dynamic_key .setText(verify_list_key.get(i));
			verify_dtextView_dynamic_value.setText(verify_list_value.get(i));
			verify_lineraylayout_main.addView(verify_linearlayout_dynamic);
		}
	}




}
